#!/usr/bin/env python
# -*- coding:utf-8 -*-
from excel.EGI.EgiObj import EgiObj


class EgiBaseTabTop:

    def __init__(self):
        return

    # 计算 Valid ANB
    # Params:
    # 翌年歲 D3 : anb
    # 供款期 D14 : Payment Term
    def get_Valid_ANB(self, anb, payment_term):
        # 参考计算公式
        # =D3+D14<=76
        return (anb + payment_term) <= 76

    # 计算 EGI index D17
    # Params:
    def get_EGI_index(self, ):
        # 参考计算公式
        # 169
        return 169

    # 计算 PLCS index 2
    # Params:
    # EGI index D17 : EGI index
    def get_PLCS_index_2(self, ):
        # 参考计算公式
        # =D17+1
        return self.get_EGI_index() + 1

    # 计算 5Y rate
    # Params:
    def get_5Y_rate(self, ):
        # 参考计算公式
        # 493
        return 493

    # 计算 10Y rate
    # Params:
    def get_10Y_rate(self, ):
        # 参考计算公式
        # 265
        return 265

    # 计算 USD target premium
    # Params:
    # 儲蓄金額 H3 : Saving
    # Display rate D27 : Display rate
    def get_USD_target_premium(self, saving, display_rate):
        # 参考计算公式
        # H3/D27
        return saving / display_rate

    # 计算 Actual annual contribution
    # Params:
    # USD target premium D21 : USD target premium
    def get_Actual_annual_contribution(self, usd_target_premium):
        # 参考计算公式
        # =ROUNDUP(D21/100,0)*100
        cal = round2precision(usd_target_premium / 100, 0, 'up')
        return int(cal) * 100

    # 计算 Actual ratio
    # Params:
    # Actual annual contribution D22 : Actual annual contribution
    # EGI index D17 : EGI index in Excel Data
    def get_Actual_ratio(self, actual_annual_contribution):
        # 参考计算公式
        # =D22/VLOOKUP("SA",data4!A2:HI103,D17,FALSE)
        return actual_annual_contribution / EgiObj.EGI_169_Index_std

    def get_Chi(self, C187):
        # 参考计算公式
        # IF(LOWER(C187)="chi","Yes","No")
        return "Yes" if C187 == "chi" else "No"

    # 计算 Exchange rate
    # Params:
    # 貨幣 D4 : Currency
    def get_Exchange_rate(self, currency):
        # 参考计算公式
        # IF(LOWER(D4)="hkd",8,1)
        return 8 if str(currency).lower() == "hkd" else 1

    # 计算 Display rate
    # Params:
    # 貨幣 D4 : Currency
    def get_Display_rate(self, currency):
        # 参考计算公式
        # =IF(LOWER(D4)="rmb-u",Notes!B2,IF(LOWER(D4)="hkd-u",7.8,1))
        if str(currency).lower() == "rmb":
            return EgiObj.USD_2_RMB_RATE
        elif str(currency).lower() == "hkd":
            return EgiObj.HKD_2_RMB_RATE
        return EgiObj.USD_RATE

    # 计算 Underlying currency
    # Params:
    # 貨幣 D4 : Currency
    def get_Underlying_currency(self, currency):
        # 参考计算公式
        # =IF(LOWER(D4)="rmb-u","(USD)",IF(LOWER(D4)="hkd-u","(USD)",IF(LOWER(D4)="hkd","(HKD)",IF(LOWER(D4)="usd","(USD)"))))
        if str(currency).lower() == "rmb":
            return "(USD)"
        elif str(currency).lower() == "hkd":
            return "(HKD)"
        elif str(currency).lower() == "usd":
            return "(USD)"

    # 计算 Underlying currency
    # Params:
    # 貨幣 D4 : Currency
    def get_Display_currenty(self, currency):
        # 参考计算公式
        # =IF(LOWER(D4)="rmb-u","(RMB)",IF(LOWER(D4)="hkd-u","(HKD)",IF(LOWER(D4)="hkd","(HKD)",IF(LOWER(D4)="usd","(USD)"))))
        return EgiObj.switch_C10_dict(currency)


def round2precision(val, precision: int = 0, which: str = ''):
    import math
    assert precision >= 0
    val *= 10 ** precision
    round_callback = round
    if which.lower() == 'up':
        round_callback = math.ceil
    if which.lower() == 'down':
        round_callback = math.floor
    return '{1:.{0}f}'.format(precision, round_callback(val) / 10 ** precision)
